/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dp1.pucp.Compras.VIsta;


import com.dp1.pucp.Modelo.Clases.*;
import com.dp1.pucp.Util.Validaciones;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author daniek
 */
public class BuscarMateriaPrimaGUI extends javax.swing.JDialog {

    /**
     * Creates new form UsuarioGUI
     */
    private JFrame padre;
    public RecetaPfMp recetaPfMp = new RecetaPfMp();
    public BuscarMateriaPrimaGUI materiaPrima;
    public ArrayList<Tipomerma> listaMerma = new ArrayList<>();
    public Tipoproducto tipoproducto = new Tipoproducto();
    public ArrayList<Tipoproducto> listaProductos = new ArrayList<>();
    
    private JTable tblResultados;
    private DefaultTableModel modelo;
    private JScrollPane scroll;
    private Border empty;

    public JFrame getPadre() {
        return padre;
    }

    public BuscarMateriaPrimaGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        this.setTitle("Busqueda de Materia Prima");
        try {
            setIconImage(new ImageIcon(getClass().getResource("../../../../../Imagenes/hombre_icono.png")).getImage());
        } catch (Exception ex) {
        }
    }

    public int getFilaSel() {
        if (tblResultados != null) {
            return tblResultados.getSelectedRow();
        } else {
            return -1;
        }
    }

    /*  public void mostrarAviso(String msn){
     Validaciones.mostrarMensajeAvisoVal(msn);   
     }
     public void mostrarError(String msn){
     Validaciones.mostrarMensajeErrorVal(msn);           
     }
     public int solicitarConfirmacion(String msn){
     return Validaciones.solicitarConfirmacionVal(msn);
     }    */
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnBuscar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtCosto = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        dtcFechaInicio = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        dtcFechaFin = new com.toedter.calendar.JDateChooser();
        jLabel5 = new javax.swing.JLabel();
        cmbMoneda = new javax.swing.JComboBox();
        btnSeleccionar = new javax.swing.JButton();
        panelTabla = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Buscar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 15))); // NOI18N

        btnBuscar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/buscar.png"))); // NOI18N
        btnBuscar.setText("Buscar");
        btnBuscar.setBorderPainted(false);
        btnBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBuscar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        btnLimpiar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/clear.png"))); // NOI18N
        btnLimpiar.setText("Limpiar");
        btnLimpiar.setBorderPainted(false);
        btnLimpiar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnLimpiar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnLimpiar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jLabel1.setText("Nombre:");

        jLabel2.setText("Costo:");

        jLabel3.setText("Fecha Inicio:");

        jLabel4.setText("Fecha Fin:");

        jLabel5.setText("Moneda:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5)
                            .addComponent(jLabel3))
                        .addGap(217, 217, 217))
                    .addComponent(btnBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLimpiar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(135, 135, 135)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cmbMoneda, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtNombre)
                            .addComponent(dtcFechaInicio, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE))
                        .addGap(18, 25, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dtcFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCosto, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(30, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtCosto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(dtcFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dtcFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel5))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(cmbMoneda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnLimpiar)
                    .addComponent(btnBuscar)))
        );

        btnSeleccionar.setFont(new java.awt.Font("Ubuntu", 0, 10)); // NOI18N
        btnSeleccionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/checkmark.png"))); // NOI18N
        btnSeleccionar.setText("Seleccionar");
        btnSeleccionar.setBorderPainted(false);
        btnSeleccionar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSeleccionar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnSeleccionar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarActionPerformed(evt);
            }
        });

        panelTabla.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 574, Short.MAX_VALUE)
                        .addComponent(btnSeleccionar))
                    .addComponent(panelTabla, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelTabla, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSeleccionar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnSeleccionarActionPerformed
    //public void addBuscarMPListener(ActionListener ml){ this.btnBuscarMP.addActionListener(ml); }

    public void LimpiarDatos() {
        panelTabla.removeAll();
        panelTabla.setBorder(empty);
        panelTabla.updateUI();
    }

    //public void addNuevoListener(ActionListener ml){ this.btnCrear.addActionListener(ml); }
    public void addBuscarListener(ActionListener ml) {
        this.btnBuscar.addActionListener(ml);
    }

    //public void addBuscarPresListener(ActionListener ml){ this.btnBurcarPres.addActionListener(ml); }
    public void addSeleccionListener(ActionListener ml) {
        this.btnSeleccionar.addActionListener(ml);
    }

    public void addLimpiarListener(ActionListener ml) {
        this.btnLimpiar.addActionListener(ml);
    }

    public void addGuardarListener(ActionListener ml) {
        this.btnBuscar.addActionListener(ml);
    }

    public void llenaGrilla(ArrayList<Tipoproducto> listTipoProducto) {
        panelTabla.removeAll();
        TitledBorder tituloResultado = BorderFactory.createTitledBorder(empty, "Resultado:");
        tituloResultado.setTitleJustification(TitledBorder.LEFT);
        panelTabla.setBorder(BorderFactory.createTitledBorder("Resultados"));
        panelTabla.updateUI();
        if (listTipoProducto != null) {
            if (!listTipoProducto.isEmpty()) {
                modelo = new DefaultTableModel() {
                    @Override
                    public boolean isCellEditable(int fila, int columna) {
                        return false;
                    }
                };

                modelo.setRowCount(listTipoProducto.size());
                Object nombreCols[] = new Object[4];
                nombreCols[0] = "";
                nombreCols[1] = "Nombre";
                nombreCols[2] = "Max Política";
                nombreCols[3] = "Min Política";
                modelo.setColumnIdentifiers(nombreCols);


                for (int i = 0; i < listTipoProducto.size(); i++) {
                    if (i < 9) {
                        modelo.setValueAt("0" + (i + 1), i, 0);
                    } else {
                        modelo.setValueAt(i + 1, i, 0);
                    }
                    modelo.setValueAt(listTipoProducto.get(i).getNombre(), i, 1);
                    modelo.setValueAt(listTipoProducto.get(i).getMinPolitica(), i, 2);
                    modelo.setValueAt(listTipoProducto.get(i).getMaxPolitica(), i, 3);

                    if (listTipoProducto.get(i).getEstado() == 1) {
                        modelo.setValueAt("Activo", i, 5);
                    }

                    if (listTipoProducto.get(i).getEstado() == 2) {
                        modelo.setValueAt("Inactivo", i, 5);
                    }

                }

                tblResultados = new JTable();
                tblResultados.setDragEnabled(false);
                tblResultados.setModel(modelo);
                TableColumn tcol = tblResultados.getColumn("");
                tcol.setPreferredWidth(30);
                tcol.setMinWidth(30);
                tcol.setMaxWidth(30);
                tblResultados.getColumnModel().getColumn(0).setCellRenderer(tblResultados.getTableHeader().getDefaultRenderer());
                tblResultados.setEnabled(true);
                tblResultados.setRowSelectionAllowed(true);
                scroll = new JScrollPane(tblResultados);
                panelTabla.add(scroll);

            } else {
                panelTabla.removeAll();
                panelTabla.setBorder(empty);
                Validaciones.mostrarMensajeAvisoVal("No se encontraron resultados.");
            }
        } else {
            panelTabla.removeAll();
            panelTabla.setBorder(empty);
            Validaciones.mostrarMensajeAvisoVal("No se encontraron resultados.");
        }

        panelTabla.updateUI();
    }

//    public void llenaGrilla(ArrayList<Tipoproducto> lista) {
//        if (lista != null) {
//            ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
//            for (int i = 0; i < lista.size(); i++) {
//                ((DefaultTableModel) tblResultados.getModel()).setRowCount(tblResultados.getRowCount() + 1);
//                tblResultados.setValueAt(lista.get(i).getNombre(), i, 0);
//                tblResultados.setValueAt(lista.get(i).getMinPolitica(), i, 1);
//                tblResultados.setValueAt(lista.get(i).getMaxPolitica(), i, 2);
//
//            }
//        }
//    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnSeleccionar;
    private javax.swing.JComboBox cmbMoneda;
    private com.toedter.calendar.JDateChooser dtcFechaFin;
    private com.toedter.calendar.JDateChooser dtcFechaInicio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel panelTabla;
    private javax.swing.JTextField txtCosto;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
